package com.java.system.controller;

import cn.hutool.core.util.StrUtil;
import com.java.common.utils.PageUtils;
import com.java.common.utils.R;
import com.java.common.utils.UuidUtil;
import com.java.system.entity.RoleResource;
import com.java.system.service.RoleResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/admin/roleResource")
public class RoleResourceController {

    @Autowired
    private RoleResourceService roleResourceService;

    /**
     * 带条件分页查询角色资源关联
     */
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = roleResourceService.queryPage(params);

        return R.ok(page);
    }

    /**
     * 保存，id有值时更新，无值时新增
     */
    @PostMapping("/save")
    public R save(@RequestBody RoleResource roleResource) {
        if (StrUtil.isNotBlank(roleResource.getId())) {
            roleResourceService.updateById(roleResource);
        } else{
            roleResource.setId(UuidUtil.getShortUuid());
            roleResourceService.save(roleResource);
        }
        return R.ok(roleResource);
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable String id) {
        roleResourceService.removeById(id);
        return R.ok();
    }
}
